import json

from django.http import HttpResponse
from django.shortcuts import render, redirect

from .models import Teacher

# Create your views here.
""" 
request 参数  表示请求对象 
1. 通过request获取参数
  1.1  获取请求方式   method  get  post    /app04/add  get  add.html  post 数据的添加
  1.2 获取请求参数  
       querystring   /app04/add  
           ?name=tom&age=23    request.GET
           body  form-data     request.POST
           
       Json 格式数据  轻量级数据传输格式  {"key":"value"}     POST方式的请求携带 
            request.body
        
       path          /app04/delete/1

def add(request):
    # 获取请求方式
    method = request.method
    print(method)
    # 获取请求参数
    print('type request.GET', type(request.GET))
    if method == 'GET':
        name = request.GET.get('name', default='xiaoming')  # 参数没有传递  返回None
        # name = request.GET['name']  # 参数必须要传递  否则抛出异常
        age = request.GET.get('age')
        salary = request.GET.get('salary')
        print(name, age, salary)
    elif method == 'POST':
        name = request.GET.get('name')
        age = request.GET.get('age')
        salary = request.GET.get('salary')
        print(name, age, salary)
        print('------------------------------------')
        name = request.POST.get('name')
        age = request.POST.get('age')
        salary = request.POST.get('salary')
        print(name, age, salary)

    return HttpResponse('<h1>add ok</h1>')


def update(request):
    if request.method == 'POST':
        # 获取前端传递的json格式数据
        json_data = request.body.decode('utf-8')
        print(json_data, type(json_data))
        # json_str  -> dict
        dict_data = json.loads(json_data)
        print(dict_data, type(dict_data))

    return HttpResponse('<h1>update ok</h1>')


def delete(request, id):
    print('id = ', id)
    return HttpResponse('<h1>delete ok</h1>')

"""


def index(request):
    if request.method == 'GET':
        # 查询数据库  展示所有教师的信息  [{},{}.{}]
        teachers = Teacher.objects.all().values().order_by('-id')
        contex = {
            'teachers': teachers
        }
        #  页面跳转
        return render(request, 'index.html', context=contex)


# 新增   /app04/add   get  ->  html   post -> insert
def add_teacher(request):
    if request.method == 'GET':
        return render(request, 'teacher_add.html')

    # 获取表单参数
    name = request.POST.get('name')
    age = request.POST.get('age')
    salary = request.POST.get('salary')
    tel = request.POST.get('tel')
    print(name, age, salary, tel)

    # Model   save   response
    teacher = Teacher(name=name, age=age, salary=salary, tel=tel)
    # 保存数据
    teacher.save()
    # 浏览器重定向
    return redirect('/app04/index')


# 编辑
def edit_teacher(request):
    if request.method == 'GET':
        # 根据id 查询教师信息
        id = request.GET.get('id')
        teacher = Teacher.objects.filter(id=id).first()
        context = {
            "id": teacher.id,
            'name': teacher.name,
            'age': teacher.age,
            'salary': teacher.salary,
            'tel': teacher.tel
        }
        return render(request, 'teacher_edit.html', context)

        # 获取表单参数
    id = request.POST.get('id')
    name = request.POST.get('name')
    age = request.POST.get('age')
    salary = request.POST.get('salary')
    tel = request.POST.get('tel')
    print(id, name, age, salary, tel)

    # # Model   save   response
    # 更新数据  id
    Teacher.objects.filter(id=id).update(name=name, age=age, salary=salary, tel=tel)
    # 浏览器重定向
    return redirect('/app04/index')


def delete_teacher(request, id):
    # 根据id删除数据
    Teacher.objects.filter(id=id).delete()
    # 浏览器重定向
    return redirect('/app04/index')
